<?php

namespace App\Services\Balance;

use App\Models\Balance\Balance as ModelsBalance;
use App\Models\Balance\BalanceLog as ModelsBalanceLog;
// use Exception;
use Illuminate\Support\Facades\DB;

class Balance
{
    /**
     * 初始化余额变化，更改客户余额
     * @param [type] $custom_id 客户资料
     * @param [type] $balance 金额
     */

    public static function resetBalance($custom_id, $balance, $changeby_manger_id, $cpid)
    {
        $custom_balance = ModelsBalance::where(['custom_id' => $custom_id, 'cpid' => $cpid])->first();
        if (!$custom_balance) {
            throw new \Exception('账户不存在');
        }

        DB::beginTransaction();
        try {
            ModelsBalance::where(['custom_id' => $custom_id])->update(['balance' => DB::raw('balance +' .$balance), 'balance_init' => $balance,'balance_init_at'=>time(), 'update_at' => time()]);
            $data = [
                'cpid' => $custom_balance->cpid,
                'category' => $custom_balance->category,
                'custom_id' => $custom_balance->custom_id,
                'company_name' => $custom_balance->company_name,
                'manger_id' => $custom_balance->manger_id,
                'manger_realname' => $custom_balance->manger_realname,
                'changeby_manger_id' => $changeby_manger_id,
                'is_lock' => 1,
                'logtype' => $custom_balance->category == 1 ? 11 : 1,
                'logtitle' => '更改期初余额，余额从' . $custom_balance->balance . '变更为' . mathfuncmoney('add',$custom_balance->balance,$balance),
                'balance_change' => $balance,
                'balance_before' => $custom_balance->balance,
                'balance_after' => mathfuncmoney('add',$custom_balance->balance,$balance),
                'create_at' => time()

            ];
            ModelsBalanceLog::create($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception('更改余额失败' . $e->getMessage());
        }
    }

    /**
     * 采购充值
     */
    public static function purchaseRecharge($custom_id,$cpid,$balance,$changeby_manger_id){
        $custom_balance = ModelsBalance::where(['custom_id' => $custom_id, 'cpid' => $cpid])->first();
        if (!$custom_balance) {
            throw new \Exception('账户不存在');
        }

        DB::beginTransaction();
        try {
            ModelsBalance::where(['custom_id' => $custom_id])->update(['balance' => DB::raw('balance +' .$balance), 'update_at' => time()]);
            $data = [
                'cpid' => $custom_balance->cpid,
                'category' => $custom_balance->category,
                'custom_id' => $custom_balance->custom_id,
                'company_name' => $custom_balance->company_name,
                'manger_id' => $custom_balance->manger_id,
                'manger_realname' => $custom_balance->manger_realname,
                'changeby_manger_id' => $changeby_manger_id,
                'is_lock' => 1,
                'logtype' => 2,
                'logtitle' => '充值打款金额：' . $balance . '充值后变更为' .  mathfuncmoney('add',$custom_balance->balance,$balance),
                'balance_change' => $balance,
                'balance_before' => $custom_balance->balance,
                'balance_after' => mathfuncmoney('add',$custom_balance->balance,$balance),
                'create_at' => time()

            ];
            ModelsBalanceLog::create($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception('更改余额失败' . $e->getMessage());
        }
    }

    /**
     * 销售退款
     * 1.删除销售订单退款
     *
     */
    public static function SaleRefund($custom_id,$cpid,$balance,$changeby_manger_id,$scene='1'){

        $custom_balance = ModelsBalance::where(['custom_id' => $custom_id, 'cpid' => $cpid])->first();
        if (!$custom_balance) {
            throw new \Exception('账户不存在');
        }

        DB::beginTransaction();
        try {
            ModelsBalance::where(['custom_id' => $custom_id])->update(['balance' => DB::raw('balance +' .$balance), 'update_at' => time()]);
            $data = [
                'cpid' => $custom_balance->cpid,
                'category' => $custom_balance->category,
                'custom_id' => $custom_balance->custom_id,
                'company_name' => $custom_balance->company_name,
                'manger_id' => $custom_balance->manger_id,
                'manger_realname' => $custom_balance->manger_realname,
                'changeby_manger_id' => $changeby_manger_id,
                'is_lock' => 1,
                'logtype' => 2,
                'logtitle' => '删除销售订单退款金额：' . $balance . '退款后变更为' .  mathfuncmoney('add',$custom_balance->balance,$balance),
                'balance_change' => $balance,
                'balance_before' => $custom_balance->balance,
                'balance_after' => mathfuncmoney('add',$custom_balance->balance,$balance),
                'create_at' => time()
            ];
            if($scene == '1'){
                $data["logtitle"] = '删除销售订单,退款金额：' . $balance . '退款后变更为' .  mathfuncmoney('add',$custom_balance->balance,$balance);
            }
            if($scene == '2'){
                $data["logtitle"] = '删除销售发货单,退款金额：' . $balance . '退款后变更为' .  mathfuncmoney('add',$custom_balance->balance,$balance);
            }
            if($scene == '3'){
                $data["logtitle"] = '审核通过销售退款单,退款金额：' . $balance . '退款后变更为' .  mathfuncmoney('add',$custom_balance->balance,$balance);
            }
            ModelsBalanceLog::create($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception('更改余额失败' . $e->getMessage());
        }
    }





    public static function purchaseRechargeSync($custom_id,$cpid,$balance,$changeby_manger_id){
        $custom_balance = ModelsBalance::where(['custom_id' => $custom_id, 'cpid' => $cpid])->first();
        if (!$custom_balance) {
            throw new \Exception('账户不存在');
        }

        DB::beginTransaction();
        try {
            ModelsBalance::where(['custom_id' => $custom_id])->update(['balance' => DB::raw('balance +' .$balance), 'update_at' => time()]);
            $data = [
                'cpid' => $custom_balance->cpid,
                'category' => $custom_balance->category,
                'custom_id' => $custom_balance->custom_id,
                'company_name' => $custom_balance->company_name,
                'manger_id' => $custom_balance->manger_id,
                'manger_realname' => $custom_balance->manger_realname,
                'changeby_manger_id' => $changeby_manger_id,
                'is_lock' => 1,
                'logtype' => 2,
                'logtitle' => '付款单打款金额：' . $balance . '充值后变更为' .  mathfuncmoney('add',$custom_balance->balance,$balance),
                'balance_change' => $balance,
                'balance_before' => $custom_balance->balance,
                'balance_after' => mathfuncmoney('add',$custom_balance->balance,$balance),
                'create_at' => time()

            ];
            ModelsBalanceLog::create($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception('更改余额失败' . $e->getMessage());
        }
    }




    public static function salerRechargeSync($custom_id,$cpid,$balance,$changeby_manger_id){
        $custom_balance = ModelsBalance::where(['custom_id' => $custom_id, 'cpid' => $cpid])->first();
        if (!$custom_balance) {
            throw new \Exception('账户不存在');
        }

        DB::beginTransaction();
        try {
            ModelsBalance::where(['custom_id' => $custom_id])->update(['balance' => DB::raw('balance +' .$balance), 'update_at' => time()]);
            $data = [
                'cpid' => $custom_balance->cpid,
                'category' => $custom_balance->category,
                'custom_id' => $custom_balance->custom_id,
                'company_name' => $custom_balance->company_name,
                'manger_id' => $custom_balance->manger_id,
                'manger_realname' => $custom_balance->manger_realname,
                'changeby_manger_id' => $changeby_manger_id,
                'is_lock' => 1,
                'logtype' => 12,
                'logtitle' => '客户余额打款金额：' . $balance . '充值后变更为' .  mathfuncmoney('add',$custom_balance->balance,$balance),
                'balance_change' => $balance,
                'balance_before' => $custom_balance->balance,
                'balance_after' => mathfuncmoney('add',$custom_balance->balance,$balance),
                'create_at' => time()

            ];
            ModelsBalanceLog::create($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception('更改余额失败' . $e->getMessage());
        }
    }

    /***
     *  销售流程,余额扣款
     *  $scene  场景1.发货单创建成功,扣款
    */
    public static function salePay($custom_id,$cpid,$balance,$changeby_manger_id,$scene='1'){
        $custom_balance = ModelsBalance::where(['custom_id' => $custom_id, 'cpid' => $cpid])->first();
        if (!$custom_balance) {
            throw new \Exception('账户不存在');
        }

        DB::beginTransaction();
        try {
            ModelsBalance::where(['custom_id' => $custom_id])->update(['balance' => DB::raw('balance +' .$balance), 'update_at' => time()]);
            $data = [
                'cpid' => $custom_balance->cpid,
                'category' => $custom_balance->category,
                'custom_id' => $custom_balance->custom_id,
                'company_name' => $custom_balance->company_name,
                'manger_id' => $custom_balance->manger_id,
                'manger_realname' => $custom_balance->manger_realname,
                'changeby_manger_id' => $changeby_manger_id,
                'is_lock' => 1,
                'logtype' => 12,
                'logtitle' => '客户余额打款金额：' . $balance . '充值后变更为' .  mathfuncmoney('add',$custom_balance->balance,$balance),
                'balance_change' => $balance,
                'balance_before' => $custom_balance->balance,
                'balance_after' => mathfuncmoney('add',$custom_balance->balance,$balance),
                'create_at' => time()

            ];
            ModelsBalanceLog::create($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception('更改余额失败' . $e->getMessage());
        }
    }


    public static function purchaseSettleRecharge($custom_id,$cpid,$balance,$changeby_manger_id){
        $custom_balance = ModelsBalance::where(['custom_id' => $custom_id, 'cpid' => $cpid])->first();
        if (!$custom_balance) {
            throw new \Exception('账户不存在');
        }

        // if($custom_balance->balance< $balance){
        //     throw new \Exception('');
        // }



        DB::beginTransaction();
        try {
            ModelsBalance::where(['custom_id' => $custom_id])->update(['balance' => DB::raw('balance -' .$balance), 'update_at' => time()]);
            $data = [
                'cpid' => $custom_balance->cpid,
                'category' => $custom_balance->category,
                'custom_id' => $custom_balance->custom_id,
                'company_name' => $custom_balance->company_name,
                'manger_id' => $custom_balance->manger_id,
                'manger_realname' => $custom_balance->manger_realname,
                'changeby_manger_id' => $changeby_manger_id,
                'is_lock' => 1,
                'logtype' => 3,
                'logtitle' => '结算金额：' . $balance . '结算后变更为' .  mathfuncmoney('sub',$custom_balance->balance,$balance),
                'balance_change' => $balance,
                'balance_before' => $custom_balance->balance,
                'balance_after' => mathfuncmoney('sub',$custom_balance->balance,$balance),
                'create_at' => time()

            ];
            ModelsBalanceLog::create($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception('更改余额失败' . $e->getMessage());
        }
    }
    /**
     * 余额变化，订单销售或采购的初始，余额变化，锁金额也变化
     *
     * @param [type] $custom_id 客户资料
     * @param [type] $balance_change 变化金额
     * @param [type] $logtype   日志类型1、采购更改期初余额，2、采购充值，3、采购消费，4、采购退款，11、客户更改期初余额，12、客户消费，13客户退款
     * @param [type] $logtitle  日志描述
     * @param string $sn_code   订单编号，初始化可不填写
     * @param string $settle_type   结算方式
     * @param string $settle_company_name   结算公司名称
     * @return void
     */
    public static function changeBalance($custom_id, $cpid, $balance, $settle_balance, $changeby_manger_id)
    {
        $custom_balance = ModelsBalance::where(['custom_id' => $custom_id])->first();
        if (!$custom_balance) {
            throw new \Exception('账户不存在');
        }

        DB::beginTransaction();
        try {
            $balance_after = $custom_balance->balance + $balance - $settle_balance;
            ModelsBalance::where(['custom_id' => $custom_id])->update(['balance' => $balance_after, 'update_at' => time()]);
            $data = [
                'cpid' => $custom_balance->cpid,
                'category' => $custom_balance->category,
                'custom_id' => $custom_balance->custom_id,
                'company_name' => $custom_balance->company_name,
                'manger_id' => $custom_balance->manger_id,
                'manger_realname' => $custom_balance->manger_realname,
                'changeby_manger_id' => $changeby_manger_id,
                'is_lock' => 1,
                'logtype' => 12,
                'logtitle' => '办款金额：' . $balance . '结算金额：' . $settle_balance . '余额后变更为：' .$balance_after ,
                'balance_change' => $balance - $settle_balance,
                'balance_before' => $custom_balance->balance,
                'balance_after' => $balance_after,
                'create_at' => time()

            ];
            ModelsBalanceLog::create($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception('更改余额失败' . $e->getMessage());
        }
    }


    /**
     * 更改锁金额，常见修改订单导致锁金额发生变化，余额对应变化
     */
    public static function changeLockBalance($custom_id, $balance_change, $logtype, $logtitle, $sn_code = '', $settle_type = '', $settle_company_name = '')
    {
    }

    /**
     * 释放锁金额，常见订单完结锁金额发生变化，余额不变化
     */
    public static function releaseLockBalance($custom_id, $balance_change, $logtype, $logtitle, $sn_code = '', $settle_type = '', $settle_company_name = '')
    {
    }
}
